﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Theme_3_ShowGrade : System.Web.UI.Page
{
    //PowerCreator.LMS.Comment.Logic.CoursewareComment commentManager = new PowerCreator.LMS.Comment.Logic.CoursewareComment();
    private string RID = "0";
    private string cid = "0";
    PowerCreator.LMS.ViewQuery.ViewQuery opvq = new PowerCreator.LMS.ViewQuery.ViewQuery();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["ResourceID"] != null)
            {
                RID = Request.QueryString["ResourceID"].ToString();
                BinderPage("R",RID);
            }
            if (Request.QueryString["ContentID"] != null)
            {
                cid = Request.QueryString["ContentID"].ToString();
                BinderPage("C",cid);
            }
        }
    }
    private void BinderPage(string type, string rid)
    {
        int sunScore = 0;
        int userNum=0;
        DataTable dt = new DataTable();
        string sql = "";

        if(type=="R")
            sql = "select distinct FKGradeUserID from TGrade where FKGradeSkillID<>0 and GradeScore<>0 and FKGradeResourceID=" + rid + "";
        else
            sql = "select distinct FKGradeUserID from TGrade where FKGradeSkillID<>0 and GradeScore<>0 and FKGradeContentGuid='" + rid + "'";
        PowerCreator.LMS.Web.Grade.GradeManager op = new PowerCreator.LMS.Web.Grade.GradeManager();
        userNum = opvq.Count(sql);
        if (userNum > 0)
        {
            if (type == "R")
                sql = "select sum(GradeScore) as sum from TGrade where FKGradeResourceID=" + rid + "";
            else
                sql = "select sum(GradeScore) as sum from TGrade where FKGradeContentGuid='" + rid + "'";
            dt = opvq.Query(sql);
            if (dt.Rows.Count > 0)
            {
                sunScore = int.Parse(dt.Rows[0][0].ToString()) / userNum;
            }
            dt.Clear();
            dt = op.QuerySkillType("");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lbl_ShowGrade.Text += dt.Rows[i]["SkillTypeName"].ToString() + "：" + getSkillSumScore(userNum, int.Parse(dt.Rows[i]["SkillTypeID"].ToString()),rid,type) + "<br />";
            }

            lbl_SumGrade.Text = "总分："+ sunScore.ToString();
        }
        else
        {
            lbl_SumGrade.Text = "总分：尚未评价";
            dt = op.QuerySkillType("");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lbl_ShowGrade.Text += dt.Rows[i]["SkillTypeName"].ToString() + "：" + "尚未评价<br />";
            }
        }
        //lbl_ShowGrade.Text = "得分：";

    }

    private string getSkillSumScore(int userNum, int SkillTypeID, string rid, string Type)
    {
        string rtValue = "0";
        DataTable dt = new DataTable();
        string sql = "";
        if (Type == "R")
            sql = "select sum(GradeScore) as sum from TGrade where FKGradeSkillID in (select SkillID from TSkill where FKSkillTypeId=" + SkillTypeID.ToString() + ") and FKGradeResourceID=" + rid + "";
        else
            sql = "select sum(GradeScore) as sum from TGrade where FKGradeSkillID in (select SkillID from TSkill where FKSkillTypeId=" + SkillTypeID.ToString() + ") and FKGradeContentGuid='" + rid + "'";
        dt = opvq.Query(sql);
        if (dt.Rows.Count > 0)
        {
            if (dt.Rows[0][0] == null || dt.Rows[0][0].ToString().Length==0)
                rtValue = "尚未评价";
            else
            {
                string ssum = dt.Rows[0][0].ToString();
                int sum=int.Parse(dt.Rows[0][0].ToString());
                rtValue = (sum / userNum).ToString();
            }
        }
        return rtValue;
    }

}
